package wolf.opensource.lcre.util;

import java.io.StringWriter;
import org.apache.log4j.Logger;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public class SimpleVelocityEngine {
	private static final Logger logger = Logger.getLogger(SimpleVelocityEngine.class);

	private static SimpleVelocityEngine sInstance = null;
	private VelocityEngine g_engine = null;

	private SimpleVelocityEngine() {
		g_engine = new VelocityEngine();
		g_engine.setProperty("class.resource.loader.class",
				"org.apache.velocity.runtime.resource.loader.StringResourceLoader");
		try {
			g_engine.init();
		} catch (Exception e) {
			logger.error("SimpleVolecityEngine() - exception ignored", e); //$NON-NLS-1$
		}
	}

	public static synchronized SimpleVelocityEngine getUniqueInstance() {
		if (sInstance == null) {
			sInstance = new SimpleVelocityEngine();
		}
		return sInstance;
	}

	protected String evaluate(String input, VelocityContext context) throws Exception {
		StringWriter writer = new StringWriter();
		g_engine.evaluate(context, writer, "", input);
		return writer.toString();
	}
}